package com.boolib.animate.framework.web;


import com.alibaba.fastjson.JSON;
import com.boolib.animate.commons.Consts;
import com.boolib.animate.commons.exception.UserAuthenticationException;
import com.boolib.animate.framework.entity.AnimateChapterComment;
import com.boolib.animate.framework.entity.AnimateUser;
import com.boolib.animate.framework.service.advice.AnimateChapterCommentService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @author nier
 * @create 2020-02-20 18:37
 */

@RequestMapping("/api/comment")
@RestController
public class AnimateCommentController {

    @Autowired
    private HttpSession httpSession;
    @Autowired
    private AnimateChapterCommentService animateChapterCommentService;


    @GetMapping("all")
    public PageInfo<AnimateChapterComment> queryChapterList(@RequestParam("workId") Long workId, @RequestParam(value = "page",defaultValue = "0")Integer page, @RequestParam(value = "size",defaultValue = "10")Integer size){

        return animateChapterCommentService.queryAll(workId,page,size);

    }

    @PostMapping("all")
    public  AnimateChapterComment  queryChapterList(@RequestBody AnimateChapterComment animateChapterComment){
        final AnimateUser current = (AnimateUser)httpSession.getAttribute("current");

        if(current==null){
            throw new UserAuthenticationException();
        }

        animateChapterComment.setUserId(current.getId());
        animateChapterComment.setChapterId(0L);
        animateChapterComment.setPraiseCount(0);
        animateChapterComment.setUserAvatar(current.getAvatar()==null?"https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png":current.getAvatar());
        animateChapterComment.setUserNickName(current.getName());

      return   animateChapterCommentService.saveAll(animateChapterComment);

    }

    @PatchMapping("praise/{id}")
    public AnimateChapterComment queryChapterList(@PathVariable("id")Long id ){
        return   animateChapterCommentService. increment(id);

    }



}
